home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / java / creator changer / creator changer project / source / imagecomponent.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  1.6 KB  |  75 lines

  1. import java.awt.Dimension;
  2. import java.awt.Component;
  3. import java.awt.Image;
  4. import java.awt.Graphics;
  5.  
  6. /**
  7.  * ImageComponent
  8.  *
  9.  * @author Levi Brown
  10.  * @version 1.0 11/2/98
  11.  */
  12. public class ImageComponent extends Component
  13. {
  14.     /**
  15.      * Constructs a default ImageComponent.
  16.      */
  17.     public ImageComponent()
  18.     {
  19.         image = null;
  20.     }
  21.  
  22.     /**
  23.      * Returns the image being displayed.
  24.      */
  25.     public Image getImage()
  26.     {
  27.         return image;
  28.     }
  29.  
  30.     /**
  31.      * Sets the image being displayed.
  32.      */
  33.     public void setImage(Image image)
  34.     {
  35.         this.image = image;
  36.         repaint();
  37.     }
  38.  
  39.     /**
  40.      * Paints this component using the given graphics context.
  41.      * This is a standard Java AWT method which typically gets called
  42.      * by the AWT to handle painting this component. It paints this component
  43.      * using the given graphics context. The graphics context clipping region
  44.      * is set to the bounding rectangle of this component and its [0,0]
  45.      * coordinate is this component's top-left corner.
  46.      *
  47.      * @param g the graphics context used for painting
  48.      * @see java.awt.Component#repaint
  49.      * @see java.awt.Component#update
  50.      */
  51.     public void paint(Graphics g)
  52.     {
  53.         if (image != null)
  54.         {
  55.             g.drawImage(image, 0, 0, this);
  56.         }
  57.     }
  58.     
  59.     /** 
  60.      * Returns the preferred size of this component.
  61.      * @see java.awt.Component#getMinimumSize
  62.      * @see LayoutManager
  63.      */
  64.     public Dimension getPreferredSize()
  65.     {
  66.         if (image == null)
  67.             return super.getPreferredSize();
  68.         else
  69.             return new Dimension(image.getWidth(this), image.getHeight(this));
  70.     }
  71.  
  72.     protected Image image;
  73. }
  74.  
  75.